import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AbstractControl, FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';

function userNameValidator(control: FormControl): { [s: string]: boolean } {
  if (!control.value.match(/^zime/)) {
    return { invalidUser: true };
  }
  return {};



}
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  title = 'biaodan';
  myForm!: FormGroup;
  id!: AbstractControl;
  userName!: AbstractControl;
  password!: AbstractControl;
  user!: Users[];

  zf!: zf[];

  constructor(private fb: FormBuilder, public http: HttpClient) {
    this.myForm = this.fb.group(
      {
        'userName': ['', Validators.compose([Validators.minLength(7), Validators.required, userNameValidator])],
        'password': ['', Validators.compose([Validators.minLength(5), Validators.required])],
        'id': []
      }
    );
    // 关联
    this.userName = this.myForm.controls['userName'];
    this.password = this.myForm.controls['password'];
    this.id = this.myForm.controls['id'];

  }





  login() {
    this.http.post("http://127.0.0.1:8080/user/login", this.myForm.value).subscribe(response => {
      console.log(response);
      var data = JSON.stringify(response);
      data = data.substring(11, 12);
      if (data == 't') {
        alert("登录成功");

        this.http.get("http://127.0.0.1:8080/user/").subscribe((response: any) => {
          console.log(response);
          this.user = response.data;
          this.zf = this.user;
        })



      }
      else {
        alert("登录失败");
        this.user = [];

      }



    })


  }



  update() {
    this.http.post('http://127.0.0.1:8080/user/updata', this.myForm.value).subscribe(
      (res: any) => {
        console.log(res)
        if (res["success"] == true) alert("修改成功")
        else alert("修改失败")
      }
    )


    this.http.get("http://127.0.0.1:8080/user/").subscribe((response: any) => {
      console.log(response);
      this.user = response.data;
      this.zf = this.user;
    })
  }



  delete() {
    this.http.post('http://127.0.0.1:8080/user/delete', this.myForm.value).subscribe(
      (res: any) => {
        console.log(res)
        if (res["success"] == true) alert("删除成功")
        else alert("删除失败")
      }
    )


    this.http.get("http://127.0.0.1:8080/user/").subscribe((response: any) => {
      console.log(response);
      this.user = response.data;
      this.zf = this.user;
    })
  }

  add() {
    this.http.post('http://127.0.0.1:8080/user/add', this.myForm.value).subscribe(
      (res: any) => {
        console.log(res)
        if (res["success"] == true) alert("添加成功")
        else alert("添加失败")
      }
    )

    this.http.get("http://127.0.0.1:8080/user/").subscribe((response: any) => {
      console.log(response);
      this.user = response.data;
      this.zf = this.user;
    })






  }



  search() {

    // this.http.get("http://127.0.0.1:8080/user/").subscribe((response: any) => {
    //   console.log(response);
    //   this.user = response.data;
    //   this.zf = this.user;
    // })



    const a = this.id.value;
    const b = this.userName.value;
    const c = this.password.value;

    for (var i = 0; i < this.user.length; i++) {
      var t1 = false;
      var t2 = false;
      var t3 = false;
      this.zf[i].f = false;
      const a1 = this.user[i].id;
      const b1 = this.user[i].name;
      const c1 = this.user[i].password;

      if (a == a1 || a == null || a == "") {


        t1 = true;
      }

      if (b == b1 || b == "" || b == null) {

        t2 = true;
      }

      if (c == c1 || c == "" || c == null) {

        t3 = true;
      }

      if (t1 == true && t2 == true && t3 == true) {

        this.zf[i].f = true;
      }






    }







  }






  onSubmit(value: any) {
    console.log(value);
  }


  ngOnInit(): void {
  }

}
export class Users {

  id: string = "";
  name: string = "";
  password: string = "";
  f: boolean = true;
}

export class zf {
  f: boolean = true;
}